package com.gitee.target123.develop.common;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.gitee.target123.develop.common.alioss.AliOss;
import com.gitee.target123.develop.common.alioss.AliOssProperties;
import com.gitee.target123.develop.common.httpclient.HttpClientPool;
import com.gitee.target123.develop.common.httpclient.HttpClientProperties;
import com.gitee.target123.develop.common.redis.RedisTemplateUtil;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
@EnableConfigurationProperties({AliOssProperties.class, HttpClientProperties.class})
public class CommonAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "ali.oss.enabled",havingValue = "true")
    public AliOss aliOss(AliOssProperties aliOssProperties){
        AliOss aliOss = new AliOss();
        aliOss.setAliOssProperties(aliOssProperties);

        //oss
        CredentialsProvider provider=new DefaultCredentialProvider(aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret());
        OSSClient ossClient = new OSSClient(aliOssProperties.getEndpoint(), provider,null);
        aliOss.setOssClient(ossClient);

        //Acs
        IClientProfile profile = DefaultProfile.getProfile(aliOssProperties.getRegionId(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());
        IAcsClient acsClient = new DefaultAcsClient(profile);
        aliOss.setAcsClient(acsClient);

        return aliOss;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "http.client.poolEnabled",havingValue = "true")
    public HttpClientPool httpClientPool(HttpClientProperties httpClientProperties){
        return new HttpClientPool(httpClientProperties.getHttpMaxPoolSize(),
                httpClientProperties.getConnectTimeout(),
                httpClientProperties.getSocketTimeout(),
                httpClientProperties.getMaxIdleTime());
    }

    @ConditionalOnClass(StringRedisTemplate.class)
    @AutoConfigureAfter(RedisAutoConfiguration.class)
    static class RedisConfig{
        @Bean
        @ConditionalOnMissingBean
        public RedisTemplateUtil redisTemplateUtil(StringRedisTemplate stringRedisTemplate){
            return new RedisTemplateUtil(stringRedisTemplate);
        }
    }
}
